
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
const int switchPin1 = 8;
int buttonState1 = 0;
int stage1 = 0;
int leftCount = 0;
const int switchPin2 = 9;
int buttonState2 = 0;
int stage2 = 0;
int rightCount = 0;
const int switchPin3 = 10;
int buttonState3 = 0;
int stage3 = 0;
int XCount = 0;
const int switchPin4 = 11;
int buttonState4 = 0;
int stage4 = 0;
int lineCount = 0;
const int switchPin5 = 12;
int buttonState5 = 0;
int stage5 = 0;
int entCount = 0;
const int switchPin6 = 13;
int buttonState6 = 0;
int stage6 = 0;
int modeCount = 0;
int X = 0;
int line = 1;
int positionX1=1,positionX2=1,positionX3=1;
int X11=1,X12=1,X21=1,X22=1,X23=1,X31=1,X32=1,X33=1,X34=1;
int count1=0,count2=0,count3=0;
int positionY1=1;
int Y1=1,Y2=1,Y3=1,Y4=1,Y5=1,Y6=1,Y7=1,Y8=1,Y9=1,Y10=1,Y11=1,Y12=1,Y13=1;
int modeCount1=0,modeCount2=0,modeCount3=0;
int Time1 = 0;

void leftButtonStage(int leftStage) {
  switch (leftStage){
    case 0 : {
     if(buttonState1 == 1) {
         stage1++;
     }
     break;
    }
    case 1 : {
      if(buttonState1 == 0){
        stage1++;
      }
      break;
    }
    case 2 : {
      leftCount = 1;
      stage1 = 0;
      break;
    }
  }    
}

void rightButtonStage(int rightStage) {
  switch (rightStage){
    case 0 : {
     if(buttonState2 == 1) {
         stage2++;
     }
     break;
    }
    case 1 : {
      if(buttonState2 == 0){
        stage2++;
      }
      break;
    }
    case 2 : {
      rightCount = 1;
      stage2 = 0;
      break;
    }
  }    
}

void XButtonStage(int XStage) {
  switch (XStage){
    case 0 : {
     if(buttonState3 == 1) {
         stage3++;
     }
     break;
    }
    case 1 : {
      if(buttonState3 == 0){
        stage3++;
      }
      break;
    }
    case 2 : {
      XCount = 1;
      stage3 = 0;
      break;
    }
  }    
}

void lineButtonStage(int lineStage) {
  switch (lineStage){
    case 0 : {
     if(buttonState4 == 1) {
         stage4++;
     }
     break;
    }
    case 1 : {
      if(buttonState4 == 0){
        stage4++;
      }
      break;
    }
    case 2 : {
      lineCount = 1;
      stage4 = 0;
      break;
    }
  }    
}

void entButtonStage(int entStage) {
  switch (entStage){
    case 0 : {
     if(buttonState5 == 1) {
         stage5++;
     }
     break;
    }
    case 1 : {
      if(buttonState5 == 0){
        stage5++;
      }
      break;
    }
    case 2 : {
      entCount++;
      stage5 = 0;
      break;
    }
  }    
}

void modeButtonStage(int modeStage) {
  switch (modeStage){
    case 0 : {
     if(buttonState6 == 1) {
         stage6++;
     }
     break;
    }
    case 1 : {
      if(buttonState6 == 0){
        stage6++;
      }
      break;
    }
    case 2 : {
      modeCount++;
      stage6 = 0;
      break;
    }
  }    
}

void setup() {
  Serial.begin(9600);
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  pinMode(switchPin1, INPUT);
  // Print a message to the LCD.
  lcd.clear();
  lcd.print("welcome!!!");
}

void loop() {
  int wTime = millis()/1000;
  if(wTime == 2) lcd.clear();
  if(wTime > 2){
    buttonState1 = digitalRead(switchPin1);
    buttonState2 = digitalRead(switchPin2);
    buttonState3 = digitalRead(switchPin3);
    buttonState4 = digitalRead(switchPin4);
    buttonState5 = digitalRead(switchPin5);
    buttonState6 = digitalRead(switchPin6);
    leftButtonStage(stage1);
    rightButtonStage(stage2);
    XButtonStage(stage3);
    lineButtonStage(stage4);
    entButtonStage(stage5);
    modeButtonStage(stage6);
    
    if(modeCount%2 == 0){
      if(modeCount1 == 0){
        lcd.clear();
        modeCount1++;
        modeCount2 = 0;
      }
      lcd.setCursor(13,0);
      lcd.print("(1)");
      if(lineCount == 1){
        line++;
        if(line > 3) line = 1;
        lineCount = 0;
      }
      if(entCount%2 == 1) {
        line = 4;
        count1=0;
        count2=0;
        count3=0;
      }
      if(entCount%2 == 0 && count1 == 0 && count2 == 0 && count3 == 0) {
        line = 1;
      }
      
      switch(line) {
        case 1:{
          if(count1 == 0){
            lcd.clear();
          }
          lcd.setCursor(0, 0);
          lcd.print("Line 1");
          if(leftCount == 1){
            positionX1--;
            if(positionX1 < 1) positionX1 = 1;
            leftCount = 0;
          }
          if(rightCount == 1){
            positionX1++;
            if(positionX1 > 2) positionX1 = 2;
            rightCount = 0;
          }
          switch(positionX1) {
            case 1: {
              lcd.setCursor(0,1);
              lcd.print(X11);
              if(XCount == 1){
                X11++;
                if(X11 > 9) X11 = 1;
                XCount = 0;
              }
              break;
            }
            case 2: {
              lcd.setCursor(2,1);
              lcd.print(X12);
              if(XCount == 1){
                X12++;
                if(X12 > 9) X12 = 1;
                XCount = 0;
              }
              break;
            }
          }
          count1++;
          count3 = 0;
          break;
        }
        case 2:{
          if(count2 == 0){
            lcd.clear();
          }
          lcd.setCursor(0, 0);
          lcd.print("Line 2");
          if(leftCount == 1){
            positionX2--;
            if(positionX2 < 1) positionX2 = 1;
            leftCount = 0;
          }
          if(rightCount == 1){
            positionX2++;
            if(positionX2 > 3) positionX2 = 3;
            rightCount = 0;
          }
          switch(positionX2) {
            case 1: {
              lcd.setCursor(0,1);
              lcd.print(X21);
              if(XCount == 1){
                X21++;
                if(X21 > 9) X21 = 1;
                XCount = 0;
              }
              break;
            }
            case 2: {
              lcd.setCursor(2,1);
              lcd.print(X22);
              if(XCount == 1){
                X22++;
                if(X22 > 9) X22 = 1;
                XCount = 0;
              }
              break;
            }
            case 3: {
              lcd.setCursor(4,1);
              lcd.print(X23);
              if(XCount == 1){
                X23++;
                if(X23 > 9) X23 = 1;
                XCount = 0;
              }
              break;
            }
          }
          count2++;
          count1 = 0;
          break;
        }
        case 3:{
          if(count3 == 0){
            lcd.clear();
          }
          lcd.setCursor(0, 0);
          lcd.print("Line 3");
          if(leftCount == 1){
            positionX3--;
            if(positionX3 < 1) positionX3 = 1;
            leftCount = 0;
          }
          if(rightCount == 1){
            positionX3++;
            if(positionX3 > 4) positionX3 = 4;
            rightCount = 0;
          }
          switch(positionX3) {
            case 1: {
              lcd.setCursor(0,1);
              lcd.print(X31);
              if(XCount == 1){
                X31++;
                if(X31 > 9) X31 = 1;
                XCount = 0;
              }
              break;
            }
            case 2: {
              lcd.setCursor(2,1);
              lcd.print(X32);
              if(XCount == 1){
                X32++;
                if(X32 > 9) X32 = 1;
                XCount = 0;
              }
              break;
            }
            case 3: {
              lcd.setCursor(4,1);
              lcd.print(X33);
              if(XCount == 1){
                X33++;
                if(X33 > 9) X33 = 1;
                XCount = 0;
              }
              break;
            }
            case 4: {
              lcd.setCursor(6,1);
              lcd.print(X34);
              if(XCount == 1){
                X34++;
                if(X34 > 9) X34 = 1;
                XCount = 0;
              }
              break;
            }
          }
          count3++;
          count2 = 0;
          break;
        }
        case 4:{
          if(X11!=8||X12!=5){
            lcd.setCursor(0,0);
            lcd.print("1:Wrong");
          }
          else{
            lcd.setCursor(0,0);
            lcd.print("1:Right");
          }
          if(X21!=2||X22!=9||X23!=3){
            lcd.setCursor(8,0);
            lcd.print("2:Wrong");
          }
          else{
            lcd.setCursor(8,0);
            lcd.print("2:Right");
          }
          if(X31!=8||X32!=7||X33!=5||X34!=4){
            lcd.setCursor(0,1);
            lcd.print("3:Wrong");
          }
          else{
            lcd.setCursor(0,1);
            lcd.print("3:Right");
          }
          break;
        }
      }
      
      if(leftCount == 1){
        lcd.setCursor(14, 1);
        lcd.print(X);
        X++;
        leftCount = 0;
      }
    }
    
    if(modeCount%2 == 1){
      if(modeCount2 == 0){
        lcd.clear();
        modeCount2++;
        modeCount1 = 0;
      }
      lcd.setCursor(13,0);
      lcd.print("(2)");
      if(leftCount == 1){
        positionY1--;
        if(positionY1 == 0) positionY1 = 14;
        leftCount = 0;
      }
      if(rightCount == 1){
        positionY1++;
        if(positionY1 == 15) positionY1 = 1;
        rightCount = 0;
      }
      Serial.print(Y1);
      Serial.println(XCount);
      switch(positionY1) {
        case 1: {
          if(modeCount3 == 0){
            lcd.clear();
            modeCount3++;
          }
          lcd.setCursor(0,0);
          lcd.print("1st Line:");
          if(XCount == 1){
            if(Y1 == 2) Y1 = 1;
            else Y1 = 2;
            XCount = 0;
          }
          lcd.setCursor(0,1);
          if(Y1 == 1) lcd.print("YES");
          if(Y1 == 2) lcd.print("NO ");
          break;
        }
        case 2: {
          lcd.setCursor(0,0);
          lcd.print("2nd Line:");
          if(XCount == 1){
            if(Y2 == 2) Y2 = 1;
            else Y2 = 2;
            XCount = 0;
          }
          lcd.setCursor(0,1);
          if(Y2 == 1) lcd.print("YES");
          if(Y2 == 2) lcd.print("NO ");
          modeCount3 = 0;
          break;
        }
        case 3: {
          lcd.setCursor(0,0);
          lcd.print("3rd Line:");
          if(XCount == 1){
            if(Y3 == 2) Y3 = 1;
            else Y3 = 2;
            XCount = 0;
          }
          lcd.setCursor(0,1);
          if(Y3 == 1) lcd.print("YES");
          if(Y3 == 2) lcd.print("NO ");
          break;
        }
        case 4: {
          lcd.setCursor(0,0);
          lcd.print("4th Line:");
          if(XCount == 1){
            if(Y4 == 2) Y4 = 1;
            else Y4 = 2;
            XCount = 0;
          }
          lcd.setCursor(0,1);
          if(Y4 == 1) lcd.print("YES");
          if(Y4 == 2) lcd.print("NO ");
          break;
        }
        case 5: {
          lcd.setCursor(0,0);
          lcd.print("5th Line:");
          if(XCount == 1){
            if(Y5 == 2) Y5 = 1;
            else Y5 = 2;
            XCount = 0;
          }
          lcd.setCursor(0,1);
          if(Y5 == 1) lcd.print("YES");
          if(Y5 == 2) lcd.print("NO ");
          break;
        }
        case 6: {
          lcd.setCursor(0,0);
          lcd.print("6th Line:");
          if(XCount == 1){
            if(Y6 == 2) Y6 = 1;
            else Y6 = 2;
            XCount = 0;
          }
          lcd.setCursor(0,1);
          if(Y6 == 1) lcd.print("YES");
          if(Y6 == 2) lcd.print("NO ");
          break;
        }
        case 7: {
          lcd.setCursor(0,0);
          lcd.print("7th Line:");
          if(XCount == 1){
            if(Y7 == 2) Y7 = 1;
            else Y7 = 2;
            XCount = 0;
          }
          lcd.setCursor(0,1);
          if(Y7 == 1) lcd.print("YES");
          if(Y7 == 2) lcd.print("NO ");
          break;
        }
        case 8: {
          lcd.setCursor(0,0);
          lcd.print("8th Line:");
          if(XCount == 1){
            if(Y8 == 2) Y8 = 1;
            else Y8 = 2;
            XCount = 0;
          }
          lcd.setCursor(0,1);
          if(Y8 == 1) lcd.print("YES");
          if(Y8 == 2) lcd.print("NO ");
          break;
        }
        case 9: {
          lcd.setCursor(0,0);
          lcd.print("9th Line:");
          if(XCount == 1){
            if(Y9 == 2) Y9 = 1;
            else Y9 = 2;
            XCount = 0;
          }
          lcd.setCursor(0,1);
          if(Y9 == 1) lcd.print("YES");
          if(Y9 == 2) lcd.print("NO ");
          break;
        }
        case 10: {
          lcd.setCursor(0,0);
          lcd.print("10th Line:");
          if(XCount == 1){
            if(Y10 == 2) Y10 = 1;
            else Y10 = 2;
            XCount = 0;
          }
          lcd.setCursor(0,1);
          if(Y10 == 1) lcd.print("YES");
          if(Y10 == 2) lcd.print("NO ");
          break;
        }
        case 11: {
          lcd.setCursor(0,0);
          lcd.print("11th Line:");
          if(XCount == 1){
            if(Y11 == 2) Y11 = 1;
            else Y11 = 2;
            XCount = 0;
          }
          lcd.setCursor(0,1);
          if(Y11 == 1) lcd.print("YES");
          if(Y11 == 2) lcd.print("NO ");
          break;
        }
        case 12: {
          lcd.setCursor(0,0);
          lcd.print("12th Line:");
          if(XCount == 1){
            if(Y12 == 2) Y12 = 1;
            else Y12 = 2;
            XCount = 0;
          }
          lcd.setCursor(0,1);
          if(Y12 == 1) lcd.print("YES");
          if(Y12 == 2) lcd.print("NO ");
          break;
        }
        case 13: {
          lcd.setCursor(0,0);
          lcd.print("13th Line:");
          if(XCount == 1){
            if(Y13 == 2) Y13 = 1;
            else Y13 = 2;
            XCount = 0;
          }
          lcd.setCursor(0,1);
          if(Y13 == 1) lcd.print("YES");
          if(Y13 == 2) lcd.print("NO ");
          break;
        }
        case 14: {
          lcd.setCursor(0,0);
          lcd.print("1-13 Lines");
          lcd.setCursor(0,1);
          if(Y1 == 1) lcd.print("Y");
          if(Y1 == 2) lcd.print("N");
          if(Y2 == 1) lcd.print("Y");
          if(Y2 == 2) lcd.print("N");
          if(Y3 == 1) lcd.print("Y");
          if(Y3 == 2) lcd.print("N");
          if(Y4 == 1) lcd.print("Y");
          if(Y4 == 2) lcd.print("N");
          if(Y5 == 1) lcd.print("Y");
          if(Y5 == 2) lcd.print("N");
          if(Y6 == 1) lcd.print("Y");
          if(Y6 == 2) lcd.print("N");
          if(Y7 == 1) lcd.print("Y");
          if(Y7 == 2) lcd.print("N");
          if(Y8 == 1) lcd.print("Y");
          if(Y8 == 2) lcd.print("N");
          if(Y9 == 1) lcd.print("Y");
          if(Y9 == 2) lcd.print("N");
          if(Y10 == 1) lcd.print("Y");
          if(Y10 == 2) lcd.print("N");
          if(Y11 == 1) lcd.print("Y");
          if(Y11 == 2) lcd.print("N");
          if(Y12 == 1) lcd.print("Y");
          if(Y12 == 2) lcd.print("N");
          if(Y13 == 1) lcd.print("Y");
          if(Y13 == 2) lcd.print("N");
          modeCount3 = 0;
          break;
        }
      }
    }
  }
}
